Page({
  /**
   * 页面的初始数据
   */
  data: {
    friendslist:[],
    id:'',
    weekarr:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
    flag:'',
    showflag:'',
    strangerflag:'',
    name:'',
    time:''
  },
  // 年月日
  formatDate: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    return year + '/' + month + '/' + day
  },
  // 年月日 时分秒
  formatDate1: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }
    var second = function () { //获取秒数
        return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    }
    return year + '/' + month + '/' + day + ' ' + hour() + ':' + minute() + ':' + second()
  },
  // 时分
  formatDate2: function (times) {
    var date = new Date(times);
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }
    return hour() + ':' + minute()
  },
  // 月日
  formatDate3: function (times) {
    var date = new Date(times);
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    return month + '月' + day + '日'
  },
  // 排序降序,置顶排序
  // property是你需要排序传入的key
  comparedate: function(property,top) {
    return function(a, b) {
      var value1 = a[property];
      var value2 = b[property];
      var top1 = a[top];
      var top2 = b[top];
      if(top1==null){
        top1=0
      }
      if(top2==null){
        top2=0
      }
      if(top1==top2){
        return Date.parse(value2) - Date.parse(value1)
      }else{
        return top2-top1
      }
    }
  },
  // 置顶
  goTop(e){
    console.log(e.currentTarget.id)
    wx.request({
      url: `http://localhost:3000/message/gettop?id=${this.data.id}&receiveid=${e.currentTarget.id}`,
      method:"GET",
      success:res=>{
        console.log(res.data)
      },
      fail:err=>{
        console.error(err)
      }
    })
    let list = this.data.friendslist;
    for(let item in list){
      if(list[item].receiveId==e.currentTarget.id){
        list[item].top = 1
      }
    }
    this.setData({
      friendslist:list
    })
    this.setData({
      friendslist:this.data.friendslist.sort(this.comparedate('date','top'))
    })
    wx.showToast({
      title: '置顶成功',
    })
  },
  // 取消置顶
  goUnTop(e){
    console.log(e.currentTarget.id)
    wx.request({
      url: `http://localhost:3000/message/getuntop?id=${this.data.id}&receiveid=${e.currentTarget.id}`,
      method:"GET",
      success:res=>{
        console.log(res.data)
      },
      fail:err=>{
        console.error(err)
      }
    })
    let list = this.data.friendslist;
    for(let item in list){
      if(list[item].receiveId==e.currentTarget.id){
        list[item].top = 0
      }
    }
    this.setData({
      friendslist:list
    })
    this.setData({
      friendslist:this.data.friendslist.sort(this.comparedate('date','top'))
    })
    wx.showToast({
      title: '取消成功',
    })
  },  
  // 删除对话
  delete(e){
    console.log(e.currentTarget.id)
    wx.request({
      url: `http://localhost:3000/message/delete?id=${this.data.id}&receiveid=${e.currentTarget.id}`,
      method:"GET",
      success:res=>{
        console.log(res.data)
      },
      fail:err=>{
        console.error(err)
      }
    })
    wx.showToast({
      title: "删除成功",
    })
    let list = this.data.friendslist;
    for(let item in list){
      if(list[item].receiveId==e.currentTarget.id){
        list[item].show = 1;//不显示
      }
    }
    this.setData({
      friendslist:list
    })
    this.isShow()
  },
  onClose(event) {
    event.detail.instance.close();
  },
  gotoTalk(e){
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: `/pages/talk/talk?receiveId=${e.currentTarget.id}&findflag=0`,
    })
  },
  isShow(){
    let list = this.data.friendslist;
    for(let item in list){
      if(list[item].show!=1){
        this.setData({
          showflag:0 //显示
        })
        break;
      }else{
        this.setData({
          showflag:1 //不显示
        })
      }
    }
    console.log(this.data.showflag)
  },
  // 一键删除所有记录
  delall(){
    wx.request({
      url: `http://localhost:3000/message/deleteall?id=${this.data.id}`,
      method:"GET",
      success:res=>{
        console.log(res.data)
      },
      fail:err=>{
        console.error(err)
      }
    })
    let list = this.data.friendslist;
    for(let item in list){
      list[item].show = 1;//不显示
    }
    this.setData({
      friendslist:list
    })
    wx.showToast({
      title: "清空成功",
    })
    this.isShow()
  },
  goTip(){
    let that = this
    wx.showModal({
      content: '清空消息列表',
        success: function (sm) {
          if (sm.confirm) {
            that.delall();
          } else if (sm.cancel) {
            wx.showToast({
              title: "取消清空",
            })
          }
        }
    })    
  },
  gotostranger(){
    wx.navigateTo({
      url: '/pages/stranger/stranger',
    })
  },
  /**
   * 生命周期函数--监听页面加载
  */
  onLoad(options) {
    this.setData({
      id:getApp().globalData.user.id
    })
    // 先给他默认为ture,整合的时候变回去
    this.setData({flag:getApp().flag});
    // this.setData({flag:true});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("sssssssssssssssssssss",this.data.name,this.data.time)
    // 朋友列表
    this.setData({
      id:getApp().globalData.user.id
    })
    // 先给他默认为ture,整合的时候变回去
    this.setData({flag:getApp().flag});
    // this.setData({flag:true});
    let date=new Date();
    date=this.formatDate(date) //截取年月日
    date=this.formatDate1(date) //形成年月日 00：00：00
    // 获取互相关注的朋友列表
    wx.request({
      url: `http://localhost:3000/message/getfriends?id=${this.data.id}`,
      method:'GET',
      success:res=>{
        var that = this
        this.setData({
          friendslist:res.data.list
        })
        console.log(res.data.list)
        this.isShow()
        for(let i=0;i<this.data.friendslist.length;i++){
          wx.request({
            url: `http://localhost:3000/message/getlastmessage?id=${this.data.id}&receiveid=${this.data.friendslist[i].receiveId}`,
            method:"GET",
            success:res=>{
              let list = this.data.friendslist;
              if(res.data.last.content.length>12){
                let newcontent = res.data.last.content.slice(0,12)+'...'
                list[i].content=newcontent;
              }else{
                list[i].content=res.data.last.content
              }
              let time = res.data.last.time //所有
              let newtime = this.formatDate(time) //截取年月日
              newtime = this.formatDate1(newtime) //年月日 00：00：00
              let date1 = new Date(date)
              let time1 = new Date(newtime)
              let day = parseInt((date1.getTime()-time1.getTime())/(1000*60*60*24))
              if(day==0){
                list[i].time = this.formatDate2(time)
              }else if(day == 1){
                list[i].time = '昨天'
              }else if(day <7 ){
                let time2 = new Date(time)
                let week = time2.getDay()
                list[i].time = this.data.weekarr[week]
              }else{
                list[i].time = this.formatDate3(time)
              }
              list[i].date = res.data.last.time
              if(list[i].top==null){
                list[i].top=0
              }
              this.setData({
                friendslist:list
              })
              if(i==this.data.friendslist.length-1){
                this.setData({
                  friendslist:this.data.friendslist.sort(that.comparedate('date','top'))
                })
                console.log(this.data.friendslist)
              }
            },
            fail:err=>{
              console.error(err)
            }
          })
        }
      },
      fail:err=>{
        console.error(err)
      }
    })
    // 是否有陌生人的私信
    wx.request({
      url: `http://localhost:3000/message/getstranger?id=${this.data.id}`,
      method:'GET',
      success:res=>{
        console.log(res.data)
        if(res.data.code==200){
          this.setData({
            strangerflag:0
          })
        }else{
          // 获取最新一条陌生人消息信息
          wx.request({
            url: `http://localhost:3000/message/getlaststranger?id=${this.data.id}`,
            method:'GET',
            success:res=>{
              console.log(res.data.last)
              let time0;
              let time = res.data.last.time //所有
              let newtime = this.formatDate(time) //截取年月日
              newtime = this.formatDate1(newtime) //年月日 00：00：00
              let date1 = new Date(date)
              let time1 = new Date(newtime)
              let day = parseInt((date1.getTime()-time1.getTime())/(1000*60*60*24))
              if(day==0){
                time0 = this.formatDate2(time)
              }else if(day == 1){
                time0 = '昨天'
              }else if(day <7 ){
                let time2 = new Date(time)
                let week = time2.getDay()
                time0 = this.data.weekarr[week]
              }else{
                time0 = this.formatDate3(time)
              }
              this.setData({
                time:time0
              })
              wx.request({
                url: `http://localhost:3000/message/getreceiveid?receiveid=${res.data.last.sendid}`,
                method:'GET',
                success:res=>{
                  this.setData({
                    name:res.data.user.username
                  })
                },
                fail:err=>{
                  console.error(err)
                }
              })
            },
            fail:err=>{
              console.error(err)
            }
          })
          this.setData({
            strangerflag:1 //有陌生人消息
          })
        }
      },
      fail:err=>{
        console.error(err)
      }
    })
  },
  login(){
    wx.navigateTo({
      url: '/pages/200/200',
    })
  }
})